package com.ctg.behavior.calc.impala.builder.userbehavior;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtCalcDate;
import com.ctg.behavior.calc.impala.report.userbehavior.CtUSReport;
import com.ctg.behavior.calc.utils.CtCalcDaysUtils;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.usersequence.CtUSCalcBody;
import com.ctg.behavior.utils.CtDateUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUSReportBuilder {

	public List<CtUSReport> doBuilder(CtUSCalcBody calcBody) {

		log.info("calcBody={}", JSON.toJSONString(calcBody));

		Integer reportIndex = 0;
		List<CtUSReport> calcReports = new ArrayList<>();

		String loadType = calcBody.getLoadType();
		String fromDate = calcBody.getFromDate();
		String toDate = calcBody.getToDate();
		log.info("loadType={},fromDate={},toDate={}", loadType, fromDate, toDate);

		if (CtCalcConsts.LOAD_TYPE_DETAIL.equalsIgnoreCase(loadType)) {
			// 用户行业系列-按明细加载

			CtUSReport oneReport = this.copyBody(calcBody);
			calcReports.add(oneReport);

			Date eventFromTime = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
			Date eventToTime = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
			eventToTime = CtDateUtils.formatDayEndTime(eventToTime);

//			oneReport.setEventFromTime(eventFromTime.getTime());
//			oneReport.setEventToTime(eventToTime.getTime());
//
//			oneReport.setEventFromDate(CtDateUtils.dateToString(eventFromTime));
//			oneReport.setEventToDate(CtDateUtils.dateToString(eventToTime));
			
			oneReport.getCalcDate().setFromDate(eventFromTime);
			oneReport.getCalcDate().setToDate(eventToTime);

			oneReport.setReportIndex(reportIndex++);

		} else if (CtCalcConsts.LOAD_TYPE_HOUR.equalsIgnoreCase(loadType)) {
			// 用户行业系列-按小时分组加载

			List<CtCalcDate> hours = CtCalcDaysUtils.listCalcHours(fromDate, toDate);

			// 当为[按小时分组加载]时, 需要根据分页信息，选取对应的report
			int startIndex = 0;
			int endIndex = 0;
			if (calcBody.getSequenceAsc()) {
				// 处理升序情况
				startIndex = (calcBody.getCurrentPage() - 1) * calcBody.getPageSize() - 1;
				startIndex = startIndex > 0 ? startIndex : 0;
				endIndex = startIndex + calcBody.getPageSize();
				endIndex = endIndex >= hours.size() ? hours.size() : endIndex;
				// 按时间升序 - 按分页信息来选取reports
				for (int index = startIndex; index < endIndex; index++) {
					CtCalcDate oneHour = hours.get(index);
					reportIndex = reportIndex + 1;
					
					CtUSReport usReport = this.buildOneReport(reportIndex, oneHour, calcBody);
					usReport.setTotal(hours.size());
					
					calcReports.add(usReport);
				}
			} else {
				// 处理降序情况
				startIndex = (calcBody.getCurrentPage() - 1) * calcBody.getPageSize();
				startIndex = hours.size() - startIndex - 1;

				endIndex = startIndex - calcBody.getPageSize();
				endIndex = endIndex > 0 ? endIndex : 0;

				// 按时间倒序 - 按分页信息来选取reports
				for (int index = startIndex; index > endIndex; index--) {
					CtCalcDate oneHour = hours.get(index);
					reportIndex = reportIndex + 1;
					
					CtUSReport usReport = this.buildOneReport(reportIndex, oneHour, calcBody);
					usReport.setTotal(hours.size());
					
					calcReports.add(usReport);
				}
			}

		}

		log.info("calcReports={}", JSON.toJSONString(calcReports));

		return calcReports;

	}

	private CtUSReport buildOneReport(Integer reportIndex, CtCalcDate oneHour, CtUSCalcBody calcBody) {

		CtUSReport oneReport = this.copyBody(calcBody);

//		Date hourDate = CtDateUtils.stringToDate(oneHour);
//		oneReport.setEventFromTime(hourDate.getTime());
//		oneReport.setEventFromDate(CtDateUtils.dateToString(hourDate));
//
//		hourDate = CtDateUtils.formatHourEndTime(hourDate);
//		oneReport.setEventToTime(hourDate.getTime());
//		oneReport.setEventToDate(CtDateUtils.dateToString(hourDate));
		
		oneReport.setCalcDate(oneHour);

		return oneReport;
	}

	private CtUSReport copyBody(CtUSCalcBody calcBody) {

		CtUSReport report = new CtUSReport();

		report.setRequestId(calcBody.getRequestId());
		report.setAppId(calcBody.getAppId());
		report.setBookmarkId(calcBody.getBookmarkId());

		report.setCurrentPage(calcBody.getCurrentPage());
		report.setLimit(calcBody.getLimit());
		report.setPageSize(calcBody.getPageSize());

		report.setUserId(calcBody.getUserId());
		report.setDeviceId(calcBody.getDeviceId());
		report.setUseDistinct(calcBody.getUseDistinct());

		report.setFromDate(calcBody.getFromDate());
		report.setToDate(calcBody.getToDate());
		report.setUnit(calcBody.getUnit());

		report.setEvents(calcBody.getEvents());
		report.setProperties(calcBody.getProperties());
		report.setSequenceAsc(calcBody.getSequenceAsc());
		report.setUseVirtualEvent(calcBody.getUseVirtualEvent());

		report.setLoadType(calcBody.getLoadType());

		return report;

	}

}
